#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import os
import math

print("执行 "*4)
templateFile = sys.argv[1]
paraList=[]
count = 2
for arg in sys.argv:
    if count>3:
        paraList.append(arg)
    count = count+1

print(templateFile)
print(paraList)

addPara = 12 - paraList.__len__()

for i in range(0,addPara):
    paraList.append("忽略")
print(paraList)

newContentList = []
with open(templateFile, "rb") as f:
    for line in f:
        line = bytes.decode(line).strip()
        print(line)
        if '[' in line:
            para = line.split('[')[1].split(']')[0]
            replaced = '[' + para + ']'

            print("第"+para+"个参数")
            print(paraList[int(para)-1])
            if '忽略'  in str(paraList[int(para)-1]):
                print("忽略这一行的内容")
                line = ""
            elif '执行' in str(paraList[int(para)-1]):

                line = line.replace(replaced,"")
            else:

                #print 用 paralist 里面的里面的 替换调 【x】
                replaced = '['+para+']'
                line = line.replace(replaced,paraList[int(para)-1])


        if line.strip().__len__()>1:
            newContentList.append(line)


print("--------")

xcount = 1
newContentList2=[]
for x in paraList:
    if  not '忽略' in x:
        rstr = "["+str(xcount)+"]"
        for y in  newContentList:
            newContentList2.append(y.replace(rstr,x))
        newContentList = newContentList2
        xcount = xcount+1
    newContentList2 = []



print(newContentList)
print("--------")
secondContent = []
for c in newContentList:
    if 'include' in c:
        newString = ""

        fname = c.split('{')[1].split('}')[0]+".ini"
        print(fname)
        pathInclude = os.getcwd()+"/include/"
        with open(pathInclude+fname)as f:
            for line in f:
                #line = bytes.decode(line).strip()

                newString = newString+line+"\n"
                c = newString

    secondContent.append(c)




print("-wwww-------")

print(secondContent)
with open("../"+sys.argv[1]+sys.argv[2]+sys.argv[3]+sys.argv[4]+"ok.ini", 'w') as f:
    for i in secondContent:
        f.write(i+"\n")

